package com.cnunicom.modules.tra.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnunicom.common.utils.Constant;
import com.cnunicom.modules.tra.vo.MebUnitStaffVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;

import com.cnunicom.modules.tra.dao.MemberDao;
import com.cnunicom.modules.tra.entity.MemberEntity;
import com.cnunicom.modules.tra.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String superiorName = (String) params.get("superiorName");
        String memberName = (String)params.get("memberName");
        String type = (String)params.get("type");

        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params,"create_time",false),
                new QueryWrapper<MemberEntity>()
                .like(StringUtils.isNotBlank(superiorName),"superior_name",superiorName)
                .like(StringUtils.isNotBlank(memberName),"name",memberName)
                .like(StringUtils.isNotBlank(type),"type",type)
        );

        return new PageUtils(page);
    }

    /**
     * 手写查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryMebUnitStaffPage(Map<String, Object> params) {

        MebUnitStaffVO vo = voHandler(params);

        IPage<MebUnitStaffVO> page = new Query<MebUnitStaffVO>().getPage(params,"create_time",false);
        List<MebUnitStaffVO> vos = this.baseMapper.queryMebUnitStaffPage((Page) params.get(Constant.PAGE), vo);
        page.setRecords(vos);
        return new PageUtils(page);
    }

    private MebUnitStaffVO voHandler(Map<String, Object> params) {

        MebUnitStaffVO vo = new MebUnitStaffVO();
        String superiorName = Objects.isNull(params.get("superiorName"))?null:params.get("superiorName").toString();
        String mebName = Objects.isNull(params.get("memberName"))?null:params.get("memberName").toString();
        String type = Objects.isNull(params.get("type")) ? null : params.get("type").toString();

        vo.setName(mebName);
        vo.setSuperiorName(superiorName);
        vo.setType(type);

        return vo;
    }

}
